package io.github.jasonxqh.domain.activity.service;

import io.github.jasonxqh.domain.activity.adapter.repository.IActivityRepository;
import io.github.jasonxqh.domain.activity.model.entity.RaffleActivityAccountEntity;
import io.github.jasonxqh.domain.activity.model.entity.SkuRechargeEntity;
import io.github.jasonxqh.domain.activity.service.armory.IActivitySkuArmory;
import io.github.jasonxqh.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.concurrent.CountDownLatch;

/**
 * @author Fuzhengwei bugstack.cn @小傅哥
 * @description 抽奖活动订单单测
 * @create 2024-03-16 11:51
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class RaffleOrderTest {
    @Resource
    private IActivityRepository activityRepository;
    @Resource
    private IRaffleActivityAccountQuotaService rafleActivityAccountQuotaService;
    @Resource
    private IRaffleActivityAccountQuotaService accountQuotaService;
    @Resource
    private IActivitySkuArmory activityArmory;
    @Before
    public void setUp() {
        log.info("装配活动：{}", activityArmory.assembleActivitySku(9011L));
    }
//    @Test
//    public void test_createSkuRechargeOrder_duplicate() {
//        SkuRechargeEntity skuRechargeEntity = new SkuRechargeEntity();
//        skuRechargeEntity.setUserId("xiaofuge");
//        skuRechargeEntity.setSku(9011L);
//        // outBusinessNo 作为幂等仿重使用，同一个业务单号2次使用会抛出索引冲突 Duplicate entry '700091009111' for key 'uq_out_business_no' 确保唯一性。
//        skuRechargeEntity.setOutBusinessNo("700091009111");
//        String orderId = accountQuotaService.createOrder(skuRechargeEntity);
//        log.info("测试结果：{}", orderId);
//    }

    @Test
    public void test_queryActivityAccount() {

        RaffleActivityAccountEntity raffleActivityAccountEntity = activityRepository.queryActivityAccountByUserIdAndActivityId("xiaofuge", 100301L);
        System.out.println(raffleActivityAccountEntity.toString());
    }
    /**
     * 测试库存消耗和最终一致更新
     * 1. raffle_activity_sku 库表库存可以设置20个
     * 2. 清空 redis 缓存 flushall
     * 3. for 循环20次，消耗完库存，最终数据库剩余库存为0
     */
//    @Test
//    public void test_createSkuRechargeOrder() throws InterruptedException {
//        for (int i = 0; i < 2000; i++) {
//            try {
//                SkuRechargeEntity skuRechargeEntity = new SkuRechargeEntity();
//                skuRechargeEntity.setUserId("xiaofuge");
//                skuRechargeEntity.setSku(9011L);
//                skuRechargeEntity.setOutBusinessNo(RandomStringUtils.randomAlphanumeric(10));
//                skuRechargeEntity.setOutBusinessNo(RandomStringUtils.randomNumeric(12));
//                String orderId = rafleActivityAccountQuotaService.createOrder(skuRechargeEntity);
//                log.info("测试结果：{}", orderId);
//            } catch (AppException e) {
//                log.warn(e.getInfo());
//            }
//        }
//
//        new CountDownLatch(1).await();
//    }
}
